<?php
/**
*
* 功能描述（代金控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlVoucher extends CtrlBase
{
	public $paramCheck = array(
		"showlist" => array(
			'status' => array('GET', '0', 'intval', 'notnegative', 's'),
			'u_start' => array('GET', '', '', 'datetime', 's'),
			'u_end' => array('GET', '', '', 'datetime', 's'),
			'prefix' => array('GET', '', 'trim', '', 's'),
			'key' => array('GET', '', 'trim', '', 's'),
			'keyword' => array('GET', '', 'trim', '', 's'),
			'page' => array('GET', '1', 'intval', 'positive'),
			'pageSize' => array('GET', 50, 'intval', 'positive'),
			'toexcel' => array('GET', 0, 'intval', 'bool'),
			'setsta' => array('GET', 0, 'intval', 'positive'),
		),
		'create' => array(
			'prefix' => array("POST", "must", "trim", "notempty"),
			'amount' => array("POST", "must", "floatval", "positive"),
			'cnt' => array("POST", "must", "intval", "positive"),
			'outtime' => array("POST", "must", "", "datetime"),
		),
	);
	public $perm = array('order', "index.php?action=login.tologin");

	function ShowList()
	{
		$setsta = $this->params['setsta'];
		if($setsta != 0)
			return $this->SetStatus($setsta);
		$obj = $this->GetModel("voucher");
		if($this->params['toexcel'] == 1)
		{
			$head = array();
			$data = $obj->GetBaseList($this->search, $this->page, $head);
			SaveExcel("代金券", $head, $data, 'down');
			$cnt = $obj->SetExport($this->search);
			$this->CoreLog('voucher_export', count($data), $cnt, '');
			return RT_NONE;
		}
		$data = $obj->GetList($this->search, $this->page, null);
		$this->assign("listdata", $data);
		return RT_TPL;
	}

	private function SetStatus($sta)
	{
		$obj = $this->GetModel("voucher");
		if($sta != 3)
		{
			echo "1";
			return RT_NONE;
		}
		$cnt = $obj->SetOpen($this->search);
		if($cnt > 0)
		{
			echo '0';
			$this->CoreLog('voucher_open', $sta, $cnt, '');
		}
		else
			echo '2';
		return RT_NONE;
	}

	function Create()
	{
		$prefix = $this->params['prefix'];
		if(strlen($prefix) != 6)
			return RT_ATTACK;
		$obj = $this->GetModel("voucher");
		$all = $this->params['cnt'];
		$cnt = $obj->Create($prefix, $this->params['amount'], $all, $this->params['outtime'], $this->user->userid);
		if($cnt == $all)
			echo "0";
		else
			echo "有异常，成功数量$cnt";
		if($cnt != 0)
			$this->CoreLog('voucher_add', $all, $cnt, $prefix.'-,金额'.$this->params['amount']);
		return RT_NONE;
	}
}

